package util

// Ceiling find the smallest elem index whose h >= target
func Ceiling[T uint | uint32 | uint64 | int | int64](arr [][]T, h T) int {
	low, high := 0, len(arr)-1
	for low <= high {
		mid := (low + high) / 2
		if arr[mid][0] >= h {
			high = mid - 1
		} else {
			low = mid + 1
		}
	}
	if high+1 >= len(arr) {
		return -1
	}
	return high + 1
}

// Floor find the biggest elem index whose h <= target
func Floor[T uint | uint32 | uint64 | int | int64](arr [][]T, h T) int {
	low, high := 0, len(arr)-1
	for low <= high {
		mid := (low + high) / 2
		if arr[mid][0] <= h {
			low = mid + 1
		} else {
			high = mid - 1
		}
	}
	return low - 1
}

// Higher find the biggest elem index whose h > target
func Higher[T uint | uint32 | uint64 | int | int64](arr [][]T, h T) int {
	low, high := 0, len(arr)-1
	for low <= high {
		mid := (low + high) / 2
		if arr[mid][0] > h {
			high = mid - 1
		} else {
			low = mid + 1
		}
	}
	if high+1 >= len(arr) {
		return -1
	}
	return high + 1
}
